package testes;

import java.util.Calendar;
import java.util.GregorianCalendar;
import logica.util.Data;
import logica.util.Data.Mes;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Teste do enum Acessorio.
 * @author Equipe localisa, usa quem preisa.
 *
 */
public class TestaData {

	private	Data dataAtual;
	private Data dataParaTeste;

	@Before
	public void antesDoTeste() throws Exception{
		dataAtual = new Data();
		dataParaTeste = new Data(10, Mes.ABRIL, 2010);
		System.out.println("Inicio de um @Test");
	}

	//Cuidado com esse teste porque ele vai depender da data do seu sistema operacional
	//Configure a data esperada com a data do seu sistema antes de rodar o teste
	@Test
	public void testaGetData(){

		GregorianCalendar calendario = new GregorianCalendar();
		String dataAtualGregorianCalendar = calendario.get(Calendar.DAY_OF_MONTH)+"/"+(calendario.get(Calendar.MONTH) + 1)+"/"+calendario.get(Calendar.YEAR);
		System.out.println(dataAtual.getData());
		Assert.assertEquals(dataAtualGregorianCalendar, dataAtual.getData());
		Assert.assertEquals(calendario.get(Calendar.DAY_OF_MONTH)+ " de " + Mes.values()[(calendario.get(Calendar.MONTH))].getExtenso() + " de "+ calendario.get(Calendar.YEAR), dataAtual.getDataPorExtenso());
	}

	@Test
	public void testaGetDia(){
		Assert.assertEquals(10, dataParaTeste.getDia());
	}

	@Test
	public void testaGetMes(){
		Assert.assertEquals(Mes.ABRIL, dataParaTeste.getMes());
	}

	@Test
	public void testaGetAno(){
		Assert.assertEquals(2010, dataParaTeste.getAno());
	}

	@Test
	public void testaSetDia()  throws Exception{
		try{
			dataParaTeste.setDia(-1);
			Assert.fail();
		}catch(Exception e){
			Assert.assertEquals("\n \t <Data invalida!>", e.getMessage());
		}

		try{
			dataParaTeste.setDia(32);
			Assert.fail();
		}catch(Exception e){
			Assert.assertEquals("\n \t <Data invalida!>", e.getMessage());
		}

		dataParaTeste.setDia(20);
		Assert.assertEquals(20, dataParaTeste.getDia());
	}

	@Test
	public void testaSetMes() throws Exception{
		try{
			dataParaTeste.setMes(0);
			Assert.fail();
		}catch(Exception e){
			Assert.assertEquals("\n \t <Data invalida!>", e.getMessage());
		}

		try{
			dataParaTeste.setMes(-1);
			Assert.fail();
		}catch(Exception e){
			Assert.assertEquals("\n \t <Data invalida!>", e.getMessage());
		}

		try{
			dataParaTeste.setMes(null);
			Assert.fail();
		}catch(Exception e){
			Assert.assertEquals("\n \t <Data invalida!>", e.getMessage());
		}

		try{
			dataParaTeste.setMes(13);
			Assert.fail();
		}catch(Exception e){
			Assert.assertEquals("\n \t <Data invalida!>", e.getMessage());
		}

		dataParaTeste.setMes(Mes.DEZEMBRO);
		Assert.assertEquals(Mes.DEZEMBRO, dataParaTeste.getMes());

	}

	@Test
	public void testaSetAno() throws Exception{
		try{
			dataParaTeste.setAno(1850);
			Assert.fail();
		}catch(Exception e){
			Assert.assertEquals("\n \t <Data invalida!>", e.getMessage());
		}

		try{
			dataParaTeste.setAno(-2);
			Assert.fail();
		}catch(Exception e){
			Assert.assertEquals("\n \t <Data invalida!>", e.getMessage());
		}

		try{
			dataParaTeste.setAno(0);
			Assert.fail();
		}catch(Exception e){
			Assert.assertEquals("\n \t <Data invalida!>", e.getMessage());
		}

		dataParaTeste.setAno(2011);
		Assert.assertEquals(2011, dataParaTeste.getAno());
	}

	@Test 
	public void testaDataLimite() {
		try{
			@SuppressWarnings("unused")
			Data nova = new Data(31,2,2010);
			Assert.fail();
		}
		catch(Exception e){
			Assert.assertEquals("\n \t <Data invalida!>", e.getMessage());
		}

		try{
			@SuppressWarnings("unused")
			Data nova = new Data(30,2,2012);
			Assert.fail();
		}
		catch(Exception e){
			Assert.assertEquals("\n \t <Data invalida!>", e.getMessage());
		}		
		
		try{
			@SuppressWarnings("unused")
			Data nova = new Data(29,2,2010);
			Assert.fail();
		}
		catch(Exception e){
			Assert.assertEquals("\n \t <Data invalida!>", e.getMessage());
		}

		try{
			@SuppressWarnings("unused")
			Data nova = new Data(29,2,2012);
		}
		catch(Exception e){
			Assert.fail();
		}
	}
	
	@Test
	public void testaGetDiffDiasEntreDatas() throws Exception {
		Data antiga = new Data(2, 10, 2010);
		Data recente = new Data(5, 10, 2010);
		Assert.assertEquals(3, Data.getDiferencaDeDiasEntreDatas(antiga, recente));
		Assert.assertEquals(-1, Data.getDiferencaDeDiasEntreDatas(recente, antiga));
		antiga = new Data(30, 12, 2009);
		recente = new Data(2, 1, 2010);
		Assert.assertEquals(3, Data.getDiferencaDeDiasEntreDatas(antiga, recente));
		Assert.assertEquals(-1, Data.getDiferencaDeDiasEntreDatas(recente, antiga));
	}

	@Test
	public void testaEquals() throws Exception{
		Data dataParaComparar = new Data(10, Mes.ABRIL, 2010);
		Data dataParaComparar1= new Data(11, Mes.ABRIL, 2010);
		Data dataParaComparar2= new Data(10, Mes.AGOSTO, 2010);
		Assert.assertTrue(dataParaTeste.equals(dataParaComparar));
		Assert.assertFalse(this.dataParaTeste.equals(dataParaComparar1));
		Assert.assertFalse(this.dataParaTeste.equals(dataParaComparar2));
	}

	@After
	public void depoisDoTeste(){
		System.out.println("Fim de um @Test");
	}

}